package com.iwords.android;

import org.apache.http.client.methods.HttpPost;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class FeedBackList  extends ListActivity {
	private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public EfficientAdapter(Context context) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);

        }

        /**
         * The number of items in the list is determined by the number of speeches
         * in our array.
         *
         * @see android.widget.ListAdapter#getCount()
         */
        public int getCount() {
        	//System.out.println("FeedBackList.EfficientAdapter.getCount() COMMENTS = "+COMMENTS);
            return COMMENTS.length();
        }

        /**
         * Since the data comes from an array, just returning the index is
         * sufficent to get at the data. If we were using a more complex data
         * structure, we would return whatever object represents one row in the
         * list.
         *
         * @see android.widget.ListAdapter#getItem(int)
         */
        public Object getItem(int position) {
        	try{
        		return COMMENTS.get(position);
        	}catch(Exception e){
        		e.printStackTrace();
        	}
        	return null;
        }

        /**
         * Use the array index as a unique id.
         *
         * @see android.widget.ListAdapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }

        /**
         * Make a view to hold each row.
         *
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.feed_back_list_item, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.feedback_list_comments_content);
                holder.user = (TextView) convertView.findViewById(R.id.feedback_list_comments_user);

                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder.
            String comments = "";
            String username = "";
            try{
            	comments = CodeUtils.decode(((JSONObject)COMMENTS.get(position)).getString("comments"));
            	username = ((JSONObject)COMMENTS.get(position)).getString("username");
            	username = "["+username+"]@"+((JSONObject)COMMENTS.get(position)).getString("createtime");
            }catch(Exception e){
            	e.printStackTrace();
            }
            holder.text.setText(comments);
            holder.user.setText(username);

            return convertView;
        }

        static class ViewHolder {
            TextView text;
            TextView user;
        }
    }

	private static JSONArray COMMENTS = new JSONArray();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feed_back_list);
        setListAdapter(new EfficientAdapter(this));
        COMMENTS = getFeedBackList();
    }
    
	/**查询用户的提意见信息。*/
	private JSONArray getFeedBackList(){
		String href = this.getResources().getString(R.string.feed_back_top10);
		HttpPost request = new HttpPost(href);
		//System.out.println("FeedBackList.getFeedBackList()! href="+href);
		try {
			JSONObject jSONObject = ServerAccessor.access(this, request);
			boolean success = jSONObject.getBoolean("isSuccess");
			if(!success){
				displayMessage(CodeUtils.decode(jSONObject.getString("message")));
			}else{
				return jSONObject.getJSONArray("defaultValue");
			}
		}catch(Exception e){
			//e.printStackTrace();
			this.displayMessage(e.getMessage());
		}
		return null;
	}
	
	private void displayMessage(String message){
		Toast t1 = Toast.makeText(this, message, Toast.LENGTH_LONG);
		t1.show();
	}
}